//
//  RSBaseWebViewController.m
//  ClientTax
//
//  Created by 侯伟 on 2020/3/13.
//  Copyright © 2020 侯伟. All rights reserved.
//

#import "RSBaseWebViewController.h"


@interface RSBaseWebViewController ()<WKNavigationDelegate,WKUIDelegate>

//导航按钮
@property(nonatomic,strong)UIBarButtonItem* backItem;
@property(nonatomic,strong)UIBarButtonItem* closeItem;

@end

@implementation RSBaseWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    [self setupWebView];
    
    if(self.URL){
        [self loadURL:self.URL];
    }
    self.navigationItem.leftBarButtonItems = @[self.backItem];
}

- (void)setupWebView{
    
    if(!_webView){
        _webView = [[RSWebView alloc] initWithFrame:self.view.bounds];
        _webView.navigationDelegate = self;
        _webView.UIDelegate = self;
        [self.view addSubview:_webView];
    }
    if (@available(iOS 11.0, *)) {
        self.webView.webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAlways;
    }
    
    [self.webView.webView addObserver:self forKeyPath:@"title" options:(NSKeyValueObservingOptionNew) context:NULL];
}

///load
- (void)loadURL:(NSURL*)url{
    [self.webView loadURL:url];
}

- (void)loadURLStr:(NSString*)urlStr{
    [self.webView loadURLString:urlStr];
}

#pragma mark -- Navigation

- (void)close{
    [self closeViewControllerAnimated:YES];
}

- (void)goBack{
    if(!self.webView.webView)
        return;
    if(self.webView.canGoBack){
        [self.webView goBack];
    }else{
        [self close];
    }
}

- (void)forward{
    if(self.webView.webView){
        if([self.webView canGoForward]){
            [self.webView goForward];
        }
    }
}


#pragma mark  -- WKNavigationDelegate

- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
    decisionHandler(WKNavigationResponsePolicyAllow);
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    [self updateNavItem];
}

- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error{
    [self updateNavItem];
}

- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error{
    [self updateNavItem];
}

#pragma nav

-(void)updateNavItem{
    
    if([self.webView canGoBack]){
        self.navigationItem.leftBarButtonItems = @[self.backItem,self.closeItem];
    }else{
        self.navigationItem.leftBarButtonItems = @[self.backItem];
    }
}

#pragma mark private

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.webView.frame = self.view.bounds;
}

#pragma mark  -- kvo
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    if ([keyPath isEqualToString:@"title"] && !self.cutsomerTitle) {
        NSString* titleStr = self.webView.title;
        self.navigationItem.title =  titleStr;
    }
}

//dealloc
- (void)dealloc{
    [self.webView.webView removeObserver:self forKeyPath:@"title"];
}

#pragma mark --lazy

- (UIBarButtonItem*)backItem{
    if(!_backItem){
        UIBarButtonItem* backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"common_back_white"] style:(UIBarButtonItemStylePlain) target:self action:@selector(goBack)];
        _backItem = backItem;
    }
    return _backItem;
}

- (UIBarButtonItem*)closeItem{
    if(!_closeItem){
        UIBarButtonItem* closeItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"common_nav_close"] style:(UIBarButtonItemStylePlain) target:self action:@selector(close)];
        _closeItem = closeItem;
    }
    return _closeItem;
}
@end


@implementation RSBaseWebViewController (Open)

+ (RSBaseWebViewController*)openUrlString:(NSString*)urlStr{
    if(!urlStr.length) return NULL;
    
    return [RSBaseWebViewController openURL:[NSURL URLWithString:urlStr]];
    
}

+ (RSBaseWebViewController*)openURL:(NSURL *)url{
    if(!url) return NULL;
    
    RSBaseWebViewController* webController = [RSBaseWebViewController new];
    webController.URL = url;
    webController.hidesBottomBarWhenPushed = YES;
    [UIViewController showViewController:webController animated:YES];
    return  webController;
}

@end

